﻿<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="../helpproject.xsl" ?>
<topic template="Default" lasteditedby="Geert" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../helpproject.xsd">
  <title translate="true">Keeping view models alive</title>
  <keywords>
    <keyword translate="true">MVVM</keyword>
    <keyword translate="true">View model lifetime</keyword>
    <keyword translate="true">View models</keyword>
  </keywords>
  <body>
    <header>
      <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Keeping view models alive</text></para>
    </header>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">In Catel, view models are automatically closed when the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_Windows_Controls_UserControl" styleclass="Normal" translate="true">UserControl</link><text styleclass="Normal" translate="true"> is unloaded from the visual tree. This is because there is no garantuee that the control will be loaded again. This works great in most of the cases, but sometimes you need more control about the lifetime of the view model. One good example is the use of the </text><text styleclass="Normal" style="font-style:italic;" translate="true">TabControl</text><text styleclass="Normal" style="font-style:normal;" translate="true">. When a tab control contains a user control with a view model, every time a new tab is selected, the controls on the previously selected tab are unloaded (and thus the view models are closed).</text></para>
    <para styleclass="Normal"><text styleclass="Normal" style="font-style:normal;" translate="true">It is possible to have more control about the lifetime of view models. To keep a view model alive, even when the view is unloaded, set the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="P_Catel_Windows_Controls_UserControl_CloseViewModelOnUnloaded" styleclass="Normal" style="font-style:normal;" translate="true">CloseViewModelOnUnloaded</link><text styleclass="Normal" style="font-style:normal;" translate="true"> property of the </text><link displaytype="text" defaultstyle="true" type="topiclink" href="T_Catel_Windows_Controls_UserControl" styleclass="Normal" translate="true">UserControl</link><text styleclass="Normal" style="font-style:normal;" translate="true"> to false in the constructor of the view:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">CloseViewModelOnUnloaded</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">=</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#ffffff;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000000;" translate="true">false</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">;</text></para>
    <para styleclass="Normal"><text styleclass="Normal" style="font-style:normal;" translate="true">The view model will now be re-used when the view is loaded into the visual tree again.</text></para>
    <para styleclass="Notes"><text styleclass="Notes" translate="true">Keep in mind that the developer is responsible for actually closing the view model</text></para>
    <para styleclass="Notes"><text styleclass="Notes" translate="true">The </text><link displaytype="text" defaultstyle="true" type="weblink" href="http://catelexamples.codeplex.com" target="_blank" styleclass="Notes" translate="true">Catel Examples repository</link><text styleclass="Notes" translate="true"> contains an example demonstrating controlling the lifetime of view models</text></para>
  </body>
</topic>
